<?php
include_once('config.php');
$testID = 1; //IDENTIFY WHICH TEST IS BEING TAKEN
$num = 1; // FOR SELECTING THE QUESTION AND MOVING THROUGHT THE QUIZ
//$correct = ""; // FOR PASSING CORRECT ANSWERS
$last = false; // FOR COMPLETION STATUS
$_SESSION['firstName'] = "";
$studentID = $_SESSION['studentID']; //FOR STUDENT ID
//$_SESSION['correct']= array(); // FOR CORRECT ANSWERS
$_SESSION['incorrect'] = array(); // FOR INCORRECT ANSWERS
$_SESSION['finished'] = 'no'; // COMPLETION STATUS
$score = $_SESSION['score'];  // FOR SCORE
$score = "0";
// FOR ALLOWING OF 2 ATTEMPTS MAX AND TO STOP PROBLEMS WITH GETTING RESULT SET

//////////////////////////////////////////////////////////////////////////////////////
//																		   GET STUDENT
//////////////////////////////////////////////////////////////////////////////////////
function getStudent() {
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);   
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else {
$query = "SELECT * FROM student WHERE username = "
. "'" . ($_SESSION['username']) . "'";
$result = mysqli_query($con, $query);
	if (!$result)
	{ 
	echo 'no result';
	}
if ($result = mysqli_query($con, $query))
  {
while ($row=mysqli_fetch_row($result))
    {
$studentID = $row[0];     
$_SESSION['studentID'] = $studentID;   
 $firstName = $row[4];     
$_SESSION['firstName'] = $firstName;   
    }
		}
}
}
//////////////////////////////////////////////////////////////////////////////////////
//																		GET QUESTION
//////////////////////////////////////////////////////////////////////////////////////
function getQuestion($questionNum) {
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);   
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

	$query= "SELECT * FROM questions WHERE itemID = $questionNum";
	$result = mysqli_query($con, $query);

		if (!$result) {
		return false;
			}
	
	//check for rows
	$numberOfRows = mysqli_num_rows($result);
	if ($numberOfRows > 0) {
	$rowContent= array();
	
	//get stuff out of the row
	while ($row = mysqli_fetch_array($result)) {
	$rowContent[] = $row;
		}
	return $rowContent;
			} 
}

///////////////////////////////////////////////////////////////////////////////////////
//																	 GET CORRECT ANSWER
///////////////////////////////////////////////////////////////////////////////////////
function checkAnswer() {
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);   
	if (mysqli_connect_errno()) {
	printf ("Connect failed: %s <br />",
	mysqli_connect_error());
	//	echo "<p class='crying'>Connection problem</p>";
	exit ();
	}
else	{
	//echo "<p class='smiling'>Connected for grade response</p>";			
	$query= "SELECT correctAnswer"
	. " FROM questions WHERE itemID = "
	. ($_POST['num']);
	$result = mysqli_query($con, $query);

	if (!$result) {
	return false;
	}

	//check for rows
	$numberOfRows = mysqli_num_rows($result);
	if ($numberOfRows > 0) {
	$rowContent= array();

	//get stuff out of the row
	while ($row = mysqli_fetch_array($result)) {
	$rowContent[] = $row;
	}
	return $rowContent;
}
	}		

		}
		
///////////////////////////////////////////////////////////////////////////////////////
//																	 GET LAST ANSWER
///////////////////////////////////////////////////////////////////////////////////////
function lastAnswer() {
$con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);   
	if (mysqli_connect_errno()) {
	printf ("Connect failed: %s <br />",
	mysqli_connect_error());
	//	echo "<p class='crying'>Connection problem</p>";
	exit ();
	}
else	{
	//echo "<p class='smiling'>Connected for grade response</p>";			
	$query= "SELECT *"
	. " FROM studentresponse WHERE itemID = "
	. ($_POST['num']-1);
	$result = mysqli_query($con, $query);

	if (!$result) {
	return false;
	}

	//check for rows
	$numberOfRows = mysqli_num_rows($result);
	if ($numberOfRows > 0) {
	$rowContent= array();

	//get stuff out of the row
	while ($row = mysqli_fetch_array($result)) {
	$rowContent[] = $row;
	}
	return $rowContent;
}
	}		

		}
		